package com.nuorui.module.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuorui.common.core.code.UserErrorCode;
import com.nuorui.common.core.domain.ResponseDTO;
import com.nuorui.common.utils.SmartBeanUtil;
import com.nuorui.module.system.mapper.UserMapper;
import com.nuorui.module.system.domain.entity.DepartmentEntity;
import com.nuorui.module.system.mapper.DepartmentMapper;
import com.nuorui.module.system.domain.form.DepartmentForm;
import com.nuorui.module.system.domain.vo.DepartmentTreeVO;
import com.nuorui.module.system.manager.DepartmentManager;
import com.nuorui.module.system.service.DepartmentService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 部门
 *
 * @author
 * @date 2021-12-14 15:01:27
 */
@Service
@AllArgsConstructor
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, DepartmentEntity> implements DepartmentService {

    private final UserMapper userMapper;

    private final DepartmentManager departmentManager;

    @Override
    public ResponseDTO getDepartmentList() {
        return ResponseDTO.ok(baseMapper.selectDepartmentList());
    }

    @Override
    public ResponseDTO getDepartmentTree() {
        List<DepartmentTreeVO> treeVOList = departmentManager.getDepartmentTree();
        return ResponseDTO.ok(treeVOList);
    }

    @Override
    public ResponseDTO saveDepartment(DepartmentForm departmentForm) {
        DepartmentEntity entity = SmartBeanUtil.copy(departmentForm, DepartmentEntity.class);

        baseMapper.insert(entity);
        departmentManager.clearCache();

        return ResponseDTO.ok();
    }

    @Override
    public ResponseDTO<String> updateDepartment(Long departmentId, DepartmentForm departmentForm) {

        if (departmentForm.getParentId() == null) {
            return ResponseDTO.userErrorParam("父级部门id不能为空");
        }
        DepartmentEntity entity = baseMapper.selectById(departmentId);
        if (entity == null) {
            return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST);
        }
        DepartmentEntity departmentEntity = SmartBeanUtil.copy(departmentForm, DepartmentEntity.class);
        departmentEntity.setSort(departmentForm.getSort());

        baseMapper.updateById(departmentEntity);
        departmentManager.clearCache();

        return ResponseDTO.ok();
    }

    @Override
    public ResponseDTO<String> deleteDepartment(Long departmentId) {
        DepartmentEntity departmentEntity = baseMapper.selectById(departmentId);
        if (null == departmentEntity) {
            return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST);
        }
        // 是否有子级部门
        int subDepartmentNum = baseMapper.countSubDepartment(departmentId);
        if (subDepartmentNum > 0) {
            return ResponseDTO.userErrorParam("请先删除子级部门");
        }

        // 是否有未删除员工
        int employeeNum = userMapper.countByDepartmentId(departmentId);
        if (employeeNum > 0) {
            return ResponseDTO.userErrorParam("请先删除部门员工");
        }
        baseMapper.deleteById(departmentId);
        departmentManager.clearCache();

        return ResponseDTO.ok();
    }

    public List<Long> selfAndChildrenIdList(Long departmentId) {
        return departmentManager.getDepartmentSelfAndChildren(departmentId);
    }


}
